export function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}

let isFormData = (v) => {
    return Object.prototype.toString.call(v) === '[object FormData]';
}

export function removeNull(option) {

    if (!option) {
        return {};
    }

    for (var attr in option) {
        if (option[attr] === null || option[attr] === '') {
            delete option[attr];
            continue;
        }
        if (typeof (option[attr]) == "object") {
            removeNull(option[attr]);
        }
    }
    return option
}

export function getCookie(key) {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(key + "=");
        if (start !== -1) {
            start = start + key.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end === -1) end = document.cookie.length;
            return unescape(document.cookie.substring(start, end));
        }
    }
    return "";
}

export function setCookie(cName, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie =
        cName +
        "=" +
        decodeURIComponent(value) +
        (expiredays == null ? "" : ";expires=" + exdate.toGMTString()) +
        ";path=/;domain=.hozonauto.com";
}


export function transformLabelData(value, list) {
    let label = '';
    list.forEach(el => {
        if (value == el.value) {
            label = el.label
        }
    });
    return label;
}


export function tansParams(params) {
    let result = ''
    for (const propName of Object.keys(params)) {
        const value = params[propName];
        let part = encodeURIComponent(propName) + "=";
        if (value !== null && typeof (value) !== "undefined") {
            if (typeof value === 'object') {
                for (const key of Object.keys(value)) {
                    if (value[key] !== null && typeof (value[key]) !== 'undefined') {
                        let params = propName + '[' + key + ']';
                        let subPart = encodeURIComponent(params) + "=";
                        result += subPart + encodeURIComponent(value[key]) + "&";
                    }
                }
            } else {
                result += part + encodeURIComponent(value) + "&";
            }
        }
    }
    return result
}


export function download(link, name) {
    if(!name){
        name=link.slice(link.lastIndexOf('/') + 1)
    }
    let eleLink = document.createElement('a')
    eleLink.download = name
    eleLink.style.display = 'none'
    eleLink.href = link
    document.body.appendChild(eleLink)
    eleLink.click()
    document.body.removeChild(eleLink)
}
// 地址下载,fileName暂无作用
export const urlDownload = (url, fileName = '下载文件') => {
    // 创建隐藏的可下载链接
    let eleLink = document.createElement('a')
    eleLink.download = fileName
    eleLink.style.display = 'none'
    eleLink.href = url
    // 触发点击
    document.body.appendChild(eleLink)
    eleLink.click()
    // 然后移除
    document.body.removeChild(eleLink)
  }

  //   时间格式转换
  export function unixTimestamp (timestamp) {
    timestamp = Number(timestamp)
    let date = new Date(timestamp)
    let Y = date.getFullYear()
    let M =
        date.getMonth() + 1 < 10
            ? '0' + (date.getMonth() + 1)
            : date.getMonth() + 1
    let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return `${Y}-${M}-${D} ${h}:${m}:${s}`
}

// 优先级转换
// 1-3普通，4-7高优，8-10紧急 
export function formatPriorityText(val){
    if(val >= 1 && val <=3){
        return '普通'
    }
    else if(val >= 4 && val <= 7){
        return '高优'
    }
    else if(val >= 8 && val <= 10){
        return '紧急'
    }else {
        return val
    }   
}
 